/**
 * Created by jiangzhiyong on 15/10/17.
 */

var Manager = {
    init: function() {
    		Manager.auto_login();
        $("#login_btn").click(Manager.login);
    },
    //自动登录，拿到sessionID
    auto_login : function() {
		//读取token，如果有就自动登录，如果没有就跳转到登录页面
		var token = $.cookie('token');
		var user_id = $.cookie('user_id');
		console.log(token)
		
		if (token == null || token == '') return false;
		var uri = "/auto-login";
		var data = {
			"token": token,
			"user_id": user_id,
		};
		var host = "http://t2.7kez.com";
		$.ajax({
		    url: host + uri,
		    type: 'post',
		    async: false,
		    data: data,
		    dataType: 'json',
		    success: function (result) {
		        if (result.code == 0) {
		        		var session_id = result.data.sid;
		        		var name = result.data.user.name;
		        		//写入session
		        		$.cookie('session_id', session_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('name', name, { expires: Number.MAX_VALUE, path: '/' });
		        		window.location.href = "index.html";
		        } else {
		        		$.cookie('token', null);
		        		alert("自动登录失败，可能用户名或密码错误，请重试!");
		        }
		    }
		});
	},
    //登录，拿到token和sessionID
    login : function() {
    		//读取token，如果有就自动登录，如果没有就跳转到登录页面
    		var mobile = $("#mobile").val();
    		if (mobile == "") {
    			alert("请填写合法手机号");
    			return false;
    		}
    		var password = $("#password").val();
    		if (password == "") {
    			alert("请填写密码");
    			return false;
    		}
		var data = {
			"mobile": mobile,
			"password": calcMD5(password + "meetclass" + password)
		};
		var host = "http://t2.7kez.com";
	    $.ajax({
	        url: host + "/login",
	        type: 'post',
	        async: false,
	        data: data,
	        dataType: 'json',
	        crossDomain:true,
	        success: function (result) {
	            /**
	             * {
					    "code": 0,
					    "data": {
					        "sid": "2de1666ab60b48da86e5f36425a449a4",
					        "token": "7ea7cd7799830ce7955e15504cb2609b",
					        "user": {
					            "birthday": 0,
					            "created_at": 1.44454829e+09,
					            "gender": 1,
					            "id": 3,
					            "mobile": "13800000003",
					            "name": "a2",
					            "nick": "a2",
					            "status": 0,
					            "type": 1
					        }
					    }
					}
	             */
	            if (result.code == 0) {
	            		//写入token、写入session
	            		var token = result.data.token;
	            		var session_id = result.data.sid;
	            		var user_id = result.data.user.id;
	            		var name = result.data.user.name;
	            		$.cookie('token', token, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('session_id', session_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('user_id', user_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('name', name, { expires: Number.MAX_VALUE, path: '/' });
	            		//跳转到首页
	            		window.location.href = "index.html";
	            } else {
	            		alert("登录失败! 用户名或密码错误");
	            }
	        }
	    });
    }
};

$(function(){
    Manager.init();
})
